from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMessageBox, QFileDialog

class UnshufflerMenuBar(QtGui.QMenuBar):
    def __init__(self, parent = None):
        QtGui.QMenuBar.__init__(self)
        self.parent = parent
        
        self.createFileMenu()
        self.createEditMenu()
        self.createViewMenu()
        self.createHelpMenu()

    def create_action(self, text, shortcut = None, tip = None, slot = None, 
                          checkable = False, duration = 3500, icon = None):
    
            action = QtGui.QAction(text, self.parent)
    
            ## Set action attributes
            if icon is not None:
                _icon = QtGui.QIcon()
                _icon.addPixmap(QtGui.QPixmap("%s.png" % icon), QtGui.QIcon.Normal, QtGui.QIcon.Off)
                action.setIcon(_icon)
                action.setIconVisibleInMenu(True)
            if shortcut is not None: 
                action.setShortcut(shortcut)
            if tip is not None: 
                action.setToolTip(tip)
                action.setStatusTip(tip)
            if checkable:
                action.setCheckable(True)
    
            ## Set action signal/slot connections
            self.connect(action, QtCore.SIGNAL('triggered()'), slot)
            self.connect(action, QtCore.SIGNAL('hovered()'), 
                         (lambda x=duration: self.parent.status_bar.showMessage(action.statusTip(), x)))
    
            return action
        
    def createEditMenu(self):    
        edit_menu = self.addMenu('&Edit')
        settings_action = self.create_action("&Settings",
                                             tip = "Change the program's settings",
                                             slot = self.parent.changeSettings,
                                             checkable = False,
                                             icon = 'data/icons/properties')
        edit_menu.addAction(settings_action)
        
    def createFileMenu(self):
        file_menu = self.addMenu('&File')
        open_action = self.create_action('&Open Image',
                                         'Ctrl+O',
                                         'Loads in image into Unshuffler',
                                         slot = self.parent.loadImage,
                                         icon = 'data/icons/search')
        quit_action = self.create_action('&Quit',
                                         'Ctrl+Q',
                                         'Close Unshuffler',
                                         slot = self.parent.close,
                                         icon = 'data/icons/close')
        file_menu.addAction(open_action)
        file_menu.addAction(quit_action)
        
    def createHelpMenu(self):
        help_menu = self.addMenu('&Help')
        about_action = self.create_action('&About',
                                          None,
                                          'About Unshuffler',
                                          slot = self.parent.about,
                                          icon = 'data/icons/help_about')
        help_action = self.create_action('&Help',
                                         'F1',
                                         'Help for users',
                                         slot = self.parent.help,
                                         icon = 'data/icons/help')
        help_menu.addAction(help_action)
        help_menu.addSeparator()
        help_menu.addAction(about_action)
        
    def createViewMenu(self):
        view_menu = self.addMenu('&View')
        console_action = self.create_action("&Console Window", tip = "View console messages and errors",
                                            slot = self.parent.console, checkable = False,
                                            icon = 'data/icons/console')
        fullscreen_action = self.create_action("&Fullscreen", "Ctrl+F", "Show the browser in full screen mode",
                                                    slot = self.parent.toggle_fullscreen, checkable = True,
                                                    icon = 'data/icons/fullscreen')
        view_menu.addAction(console_action)
        view_menu.addAction(fullscreen_action)